﻿using System;
using System.Linq.Expressions;

namespace NMF.Expressions
{
    /// <summary>
    /// This is a facade class that exposes the functionality of NMF Expressions compactly
    /// </summary>
    public static partial class Observable
    {
        /// <summary>
        /// Create an observable expression from the given LINQ Expression, i.e. the expression updates its value according to model changes
        /// </summary>
        /// <typeparam name="T">The type of the expression</typeparam>
        /// <param name="expression">The LINQ Expression tree of the expression</param>
        /// <returns>A notify value</returns>
        public static INotifyValue<T> Expression<T>(Expression<Func<T>> expression)
        {
            return new NotifyValue<T>(expression);
        }

        /// <summary>
        /// Creates a reversable and observable expression from the given LINQ expression, i.e. the expression updates its value according to model changes and any changes to the expression are propagated back to the model
        /// </summary>
        /// <typeparam name="T">The type of the expression</typeparam>
        /// <param name="expression">The LINQ Expression tree of the expression</param>
        /// <returns>A reversable notify value</returns>
        public static INotifyReversableValue<T> Reversable<T>(Expression<Func<T>> expression)
        {
            try
            {
                return new NotifyReversableValue<T>(expression);
            }
            catch (InvalidCastException)
            {
                throw new ArgumentException("The expression is not reversable.", "expression");
            }
        }
    }
}
